# coding=UTF-8
import os
import sys
import time
import requests
import json
from common.entitys.caiptal import Flow
from service.data_service import stock_dict
from utils import parse_util

father_path = os.path.abspath(os.path.dirname(os.getcwd()) + os.path.sep + ".")
sys.path.append(father_path)


# 原地址
# http://push2.eastmoney.com/api/qt/stock/get?ut=fa5fd1943c7b386f172d6893dbfba10b&invt=2&fltt=2&fields=f43,f57,f58,f169,f170,f46,f44,f51,f168,f47,f164,f163,f116,f60,f45,f52,f50,f48,f167,f117,f71,f161,f49,f530,f135,f136,f137,f138,f139,f141,f142,f144,f145,f147,f148,f140,f143,f146,f149,f55,f62,f162,f92,f173,f104,f105,f84,f85,f183,f184,f185,f186,f187,f188,f189,f190,f191,f192,f107,f111,f86,f177,f78,f110,f262,f263,f264,f267,f268,f250,f251,f252,f253,f254,f255,f256,f257,f258,f266,f269,f270,f271,f273,f274,f275,f127,f199,f128,f193,f196,f194,f195,f197,f80,f280,f281,f282,f284,f285,f286,f287,f292&secid=1.603738&cb=jQuery112408230507873719453_1592535705863&_=1592535705891
# 精简地址
# http://push2.eastmoney.com/api/qt/stock/get?ut=fa5fd1943c7b386f172d6893dbfba10b&invt=2&fltt=2&fields=f135,f136,f137,f138,f139,f141,f142,f144,f145,f147,f148,f140,f143,f146,f149&secid=1.603738&cb=jQuery112408230507873719453_1592535705863&_=1592535705891

# f135 ： 主力流入
# f136 ： 主力流出
# f137 ： 主力净流入
# f138 ： 超大单流入
# f139 ： 超大单流出
# f140 ： 超大单净流入
# f141 ： 大单流入
# f142 ： 大单流出
# f143 ： 大单净流入
# f144 ： 中单流入
# f145 ： 中单流出
# f146 ： 中单净流入
# f147 ： 小单流入
# f148 ： 小单流出
# f149 ： 小单净流入
# f168 ： 换手率
# 资金流向
def get_flow(stock_code):
    return flow(stock_code, '', '')


# 资金流向
def flow(stock_code, stock_name, stock_exchange):
    if stock_exchange == '' or stock_name == '':
        stock_name = stock_dict[stock_code]['name']
        stock_exchange = stock_dict[stock_code]['exchange']
    print("获取资金流向,股票编码" + stock_code + ':' + stock_name)
    time_stamp = int(round(time.time() * 1000))
    path_url = 'http://push2.eastmoney.com/api/qt/stock/get?ut=fa5fd1943c7b386f172d6893dbfba10b&invt=2' + \
               '&fltt=2&fields=f135,f136,f137,f138,f139,f141,f142,f144,f145,f147,f148,f140,f143,f146,f149,f168' + \
               '&secid=' + str(stock_exchange) + '.' + str(stock_code) + \
               '&cb=jQuery112408230507873719453_' + str(time_stamp) + '&_=' + str(time_stamp + 28)
    # print(path_url)
    resp = requests.get(path_url)
    resp_content = str(resp.content.decode('utf-8'))
    days_info_str = resp_content.split('(')[1].split(')')[0]
    resp_json = json.loads(days_info_str)
    resp_date = resp_json['data']
    flow = Flow()
    flow.main_i = parse_util.p_float(resp_date['f135'])
    flow.main_o = parse_util.p_float(resp_date['f136'])
    flow.main_io = parse_util.p_float(resp_date['f137'])
    flow.sb_i = parse_util.p_float(resp_date['f138'])
    flow.sb_o = parse_util.p_float(resp_date['f139'])
    flow.sb_io = parse_util.p_float(resp_date['f140'])
    flow.b_i = parse_util.p_float(resp_date['f141'])
    flow.b_o = parse_util.p_float(resp_date['f142'])
    flow.b_io = parse_util.p_float(resp_date['f143'])
    flow.m_i = parse_util.p_float(resp_date['f144'])
    flow.m_o = parse_util.p_float(resp_date['f145'])
    flow.m_io = parse_util.p_float(resp_date['f146'])
    flow.s_i = parse_util.p_float(resp_date['f147'])
    flow.s_o = parse_util.p_float(resp_date['f148'])
    flow.s_io = parse_util.p_float(resp_date['f149'])
    flow.turnover_rate = parse_util.p_float(resp_date['f168'])
    return flow


def MA(code, size):
    print("n日收盘均价")


if __name__ == '__main__':
    print("")
    # {'name': '泰晶科技', 'code': '603738', 'exchange': '1'}
    aaaa = flow('600678', '', '')
    print(aaaa.sb_io)
